1915E - Romantic Glasses - CodeForces Solution


data structures greedy math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define si set<int>
#define sll set<ll>
#define sch set<char>
#define msi multiset<int>
#define msll multiset<ll>
#define vll vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vpii vector<pair<int,int>>
#define vpic vector<pair<int, char>>
#define sti  stack<int> 
#define stc  stack<char>
#define forarr(n,a) for(int i=0;i<n;i++) cin>>a[i];
const ll M=1e9+7;
ll fact(int n){
    ll ans=1;
    for(int i=1;i<=n;i++) ans*=i;
    return ans;
}
ll NCR(int n,int r){
    return fact(n)/(fact(r)*(fact(n-r)));
}
ll GCD(ll a,ll b){
    if(a==0) return b;
    return GCD(b%a,a);
}
void solve(){
    ll n;
    cin>>n;
    vll a(n);
    forarr(n,a);
    ll sum=0;
    sll s;
    s.insert(0);
    for(int i=0;i<n;i++){
        if(i%2==0){
            sum+=a[i];
            if(s.count(sum)){
                cout<<"YES\n";
                return;
            }
            else s.insert(sum);
        }
        else{
            sum-=a[i];
            if(s.count(sum)){
                cout<<"YES\n";
                return;
            }
            else s.insert(sum);
        }
    }
    cout<<"NO\n";
}
int main(){
    ll tc;
    cin >> tc;
    //tc=1;
    while(tc--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game